package com.example.demo.api;

import com.example.demo.model.Friend;
import com.example.demo.model.FriendMapper;
import com.example.demo.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendAPI {
    @Autowired
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object friendList(HttpServletRequest request){
//        先从会话中,获取到userId
        HttpSession session=request.getSession(false);
        if(session==null){
            return new ArrayList<Friend>();
        }
        User user=(User) session.getAttribute("user");
        if(user==null){
//            当前用户没有在会话中保存
            System.out.println("用户不存在");
            return new ArrayList<Friend>();
        }
//        2.根据userId从数据库中查数据即可
        List<Friend> friendList=friendMapper.selectFriendList(user.getUserId());
        return friendList;
    }


}
